﻿$(function () {
    layer.ready(function () {
        showKouLing();//口令层
    });
    submit();//异步上传
});

//口令层,必须放在第一个不然生成的ID不符合layer.js
function showKouLing() {
    indexTemp = layer.open({
        type: 1,
        title: false,
        closeBtn: false,
        shadeClose: true,
        skin: 'yourclass',
        content: '<div style="background-color:#ffffff;text-align:center"><span style="text-align:center">请输入口令</span><p /><input type="password" id="kouLing" /><p /><input type="button" id="OK" value="确定" onclick="KouLing();" /></div>'
    });
};

function KouLing() {
    var kouling = $("#kouLing").val();
    if (kouling != "" && kouling != null) {
        $.ajax({
            type: "POst",
            cache: true,
            data: {
                data: kouling,
                type: "kouling"
            },
            url: "../ASHX/ConductModel.ashx",
            success: function (data) {
                if (data == "ok") {
                    layer.close(indexTemp);
                }
                else {
                    layer.alert("口令错误");
                    $("#kouLing").val("");
                }
            }
        });
    }
    else {
        layer.alert("请输入口令");
    }
};






//点击导入
function submit() {
    $("#import").click(function () {
        var filepath = $("#file").val();
        if (filepath == "" || filepath == null) {
            layer.alert("请选择文件");
            return false;
        }
        if (isUpload()) {
            document.getElementById("ZheZhaoDiv").style.display = "block";
            document.getElementById("loading").style.display = "block";
            $("#fileform").ajaxSubmit({
                type: 'post', // 提交方式 get/post
                success: function (data) {//程序处理成功处理代码
                    document.getElementById("ZheZhaoDiv").style.display = "none";
                    document.getElementById("loading").style.display = "none";
                    if (data == "no") {
                        layer.alert("路径为空");
                    } else if (data == "error") {
                        layer.alert("004");
                    } else if (data != null) {
                        $("#fileform").resetForm(); // 提交后用于重置表单
                        layer.alert("上传成功");
                        var source = eval("(" + data + ")");
                        var successCount = source.successCount;//成功条数
                        var failureCount = source.failureCount;//失败条数
                        var helisCount = source.helisCount;//重复条数
                        var successInfo = source.successInfo;//成功数据
                        var failureInfo = source.failureInfo;//失败数据
                        var helisInfo = source.helisInfo;//重复数据
                        var count = source.count;//总共多少条
                        document.getElementById("count").innerText = "一共" + count + "条";
                        document.getElementById("successCount").innerText = "成功了" + successCount + "条";
                        document.getElementById("failureCount").innerText = "失败了" + failureCount + "条";
                        document.getElementById("helisCount").innerText = "重复了" + helisCount + "条";
                        document.getElementById("successInfo").innerText = successInfo;
                        document.getElementById("failureInfo").innerText = failureInfo;
                        document.getElementById("helisInfo").innerText = helisInfo;

                    }
                    else {
                        layer.alert("上传失败");
                    }
                }
            });
        }
        else {
            layer.alert("您选择的文件不符合上传格式");
        }
    });
};


//验证文件格式
function isUpload() {
    //获取欲上传的文件路径
    var filepath = $("#file").val();
    if (filepath == "" || filepath == null) {
        layer.alert("请选择文件");
        return false;
    }
    //为了避免转义反斜杠出问题，这里将对其进行转换
    var re = /(\\+)/g;
    var filename = filepath.replace(re, "#");
    //对路径字符串进行剪切截取
    var one = filename.split("#");
    //获取数组中最后一个，即文件名
    var two = one[one.length - 1];
    //再对文件名进行截取，以取得后缀名
    var three = two.split(".");
    //获取截取的最后一个字符串，即为后缀名
    var last = three[three.length - 1];
    //添加需要判断的后缀名类型
    var tp = "xls|xlsx";//上传格式用|分开
    //返回符合条件的后缀名在字符串中的位置
    var type = tp.split("|");
    var isUpload = false;//是否能上传
    for (var i = 0; i < type.length; i++) {
        if (type[i] == last) {
            isUpload = true;
            break;
        }
    }
    return isUpload;//true表示符合上传格式
};
